package com.commerce.networkframework.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.commerce.networkframework.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class VolleyActivity extends AppCompatActivity {

    private Map<String, String> param = new HashMap<>();
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley);
        //创建队列
        requestQueue = Volley.newRequestQueue(VolleyActivity.this);
        NetworkImageView imageView = findViewById(R.id.volley_image);
        //缓存
        final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>(20);
        
        ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return mImageCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                mImageCache.put(url, bitmap);
            }
        });
        imageView.setImageUrl("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/baiduyun@2x-e0be79e69e.png",imageLoader);
//        stringRequest();
//        jsonRequest();
    }

    private void stringRequest(){
        //请求
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://apis.juhe.cn/simpleWeather/cityList", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //请求成功
//                TextView textView = findViewById(R.id.text_string);
//                textView.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //请求参数
                param.put("key", "7b3a2993da15202e95aa1e78e4e0799c");
                return param;
            }
        };
        requestQueue.add(stringRequest);
    }

    private void jsonRequest(){
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://apis.juhe.cn/simpleWeather/cityList", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(final JSONObject response) {
//                TextView textView = findViewById(R.id.text_string);
//                try {
//                    textView.setText(response.getString("reason"));
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(jsonObjectRequest);
    }

}